gl path "~/Dropbox/voter_id/Replication"

cap log close
log using "$path/table_a6.log", replace

use "$path/nc_dataset.dta" if new_reg == 0, clear

// drop 2018 
drop if inlist(election, 11, 12)

drop black white hispanic othernw new_reg
drop noid_p_2016 race_string race_by_year age_by_year race_by_age_by_year birth_year

* match on pre-treatment turnout path
* generate pre-treatment path
** focus only on people eligible the entire time period

** generate number of pre-treatment primaries participated in 
egen primaries = sum(voted) if mod(election,2) == 1 & election < 9, by(id)
sort id primaries
replace primaries = primaries[_n-1] if id == id[_n-1]
** generate number of pre-treatment generals participated in
egen generals = sum(voted) if mod(election,2) == 0 & election < 9, by(id)
sort id generals
replace generals = generals[_n-1] if id == id[_n-1]

sort id election

** create string encoding of possible pre-treatment outcome paths
tostring voted, generate(outcome_path)

by id: gen outcome_path_pretreat = outcome_path[1] + outcome_path[2] + ///
								outcome_path[3] + outcome_path[4] + ///
								outcome_path[5] + outcome_path[6] + ///
								outcome_path[7] + outcome_path[8]

gen count = 1

// keep general only
keep if mod(election,2) == 0


egen total_pre_treat = concat(primaries generals)

collapse (sum) count (mean) voted, by(total_pre_treat no_dmv_match election)


*** get N by path by summing together n treated and n control
by total_pre_treat: gen tot_tmp = count[6] if _n==6
egen tot_treat = sum(tot_tmp), by(total_pre_treat)
by total_pre_treat: replace tot_tmp = count[1] if _n == 1
egen tot = sum(tot_tmp), by(total_pre_treat)
drop tot_tmp
gen tot_control = tot - tot_treat

*** get weights for fw based on total n per stratum
*** each stratum has 10 obs
*** fw requires integers so need to round
gen tot2 = round(tot/10)
gen tot_treat2 = round(tot_treat/10)
egen N = sum(tot2)

egen op = group(total_pre_treat)

reghdfe voted no_dmv_match [fw=tot_treat2], a(op election)

matrix results = J(25, 4, .)

forval i = 1(1)25 {
	if `i' == 21 {
		continue
	}
	reghdfe voted no_dmv_match if op == `i' [fw=tot_treat2], a(election)
	matrix results[`i', 1] = _b[no_dmv_match]
	matrix results[`i', 2] = _se[no_dmv_match]
	qui sum tot_treat if op == `i'
	matrix results[`i', 3] = r(mean)
	qui sum tot if op == `i'
	matrix results[`i', 4] = r(mean)
}

collapse (sum) count, by(total_pre_treat)
svmat results 
keep total_pre_treat results*
renvars results1 results2 results3 results4 \ b se tot_treat tot

log close

forval i = 1(1)25 {
	local b`i' = b[`i']
	local se`i' = se[`i']
	di `b`i''
}
local b21 = . 
local se21 = .

quietly {
	cap log close
	set linesize 255
	log using "$path/table_a6.tex", text replace
	
	noisily dis "\begin{table}[t]"
	noisily dis "\centering"
	noisily dis "\caption{\textbf{Effect of Voter ID Law on 2016 General Election Turnout, by Pre-Treatment Turnout.}"
	noisily dis "\label{tab:general_heterogeneity}}"
	noisily dis "\begin{tabular}{l|ccccc}"
	noisily dis "\toprule \toprule"
	noisily dis " & \multicolumn{5}{c}{\# of Pre-Treatment General Elections}\\"
	noisily dis " & 0 & 1 & 2 & 3 & 4 \\"
	noisily dis "\midrule"
	noisily dis " \# of Pre-Treatment Primary Elections & & & & \medskip \\"
	noisily dis "0  & " %4.3f `b1' " & " %4.3f `b2' " & " %4.3f `b3' " & " %4.3f `b4' " & " %4.3f `b5' " \\"
	noisily dis " & (" %4.3f `se1' ") & (" %4.3f `se2' ") & (" %4.3f `se3' ") & (" %4.3f `se4' ") & (" %4.3f `se5' ") \smallskip\\"

	noisily dis "1  & " %4.3f `b6' " & " %4.3f `b7' " & " %4.3f `b8' " & " %4.3f `b9' " & " %4.3f `b10' " \\"
	noisily dis " & (" %4.3f `se6' ") & (" %4.3f `se7' ") & (" %4.3f `se8' ") & (" %4.3f `se9' ") & (" %4.3f `se10' ") \smallskip\\"

	noisily dis "2  & " %4.3f `b11' " & " %4.3f `b12' " & " %4.3f `b13' " & " %4.3f `b14' " & " %4.3f `b15' " \\"
	noisily dis " & (" %4.3f `se11' ") & (" %4.3f `se12' ") & (" %4.3f `se13' ") & (" %4.3f `se14' ") & (" %4.3f `se15' ") \smallskip\\"

	noisily dis "3  & " %4.3f `b16' " & " %4.3f `b17' " & " %4.3f `b18' " & " %4.3f `b19' " & " %4.3f `b20' " \\"
	noisily dis " & (" %4.3f `se16' ") & (" %4.3f `se17' ") & (" %4.3f `se18' ") & (" %4.3f `se19' ") & (" %4.3f `se20' ") \smallskip\\"

	noisily dis "4  &  & " %4.3f `b22' " & " %4.3f `b23' " & " %4.3f `b24' " & " %4.3f `b25' " \\"
	noisily dis " &  & (" %4.3f `se22' ") & (" %4.3f `se23' ") & (" %4.3f `se24' ") & (" %4.3f `se25' ") \smallskip\\"

	noisily dis "\bottomrule \bottomrule"
	noisily dis "\multicolumn{6}{p{0.9\textwidth}}{\footnotesize Each cell estimates the effect of the voter ID law on "
	noisily dis "2016 primary turnout, estimating the effect separately for different pre-treatment turnout patterns.  "
	noisily dis "We construct strata of treated and control units based on the total number of times a voter casted a ballot "
	noisily dis "in a pre-treatment primary election (2008-2014) and pre-treatment general election (2008-2014).  "
	noisily dis "We implement the same exact matching procedure described in Section \ref{sec:primary_effect}.  "
	noisily dis "Robust standard errors are in parentheses.}"
	noisily dis "\end{tabular}"
	noisily dis "\end{table}"
	
	log off
	
}



